<template>
    <div>
        <div class="operarea">
            <div class="seach">
                <el-input v-model="seachForm.name" :placeholder="$t('fabricgroupname')" clearable size="mini"></el-input>
                <el-button size="mini" icon="el-icon-search" @click="getGroupList"></el-button>
            </div>
            <div class="btnList">
                <el-button size="mini" @click="addDialogVisible = true">{{$t('add')}}</el-button>
            </div>
        </div>
        <el-card class="cardBox">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>首页</el-breadcrumb-item>
                <el-breadcrumb-item>面料组管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-table style="width: 100%" :data="groupTab" stripe v-adaptive="{bottomOffset: 85}" height="100">
                <el-table-column prop="name" :label="$t('name')">
                </el-table-column>
                <el-table-column prop="description" :label="$t('description')">
                </el-table-column>
                <el-table-column prop="address" width="150" :label="$t('operation')">
                    <template v-slot="scope">
                        <el-button @click.stop="showModify(scope.row)" size="mini">编辑</el-button>
                        <el-button @click.stop="showDel(scope.row.id)" size="mini">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                small
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :pager-count="11"
                :current-page.sync="seachForm.pageNo"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="seachForm.pageSize"
                layout="sizes, prev, pager, next"
                :total="total">
            </el-pagination>
        </el-card>
        <!-- 添加 -->
        <el-dialog :title="$t('add')" :visible.sync="addDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="addDlogClose(addDataForm)">
            <el-form :model="addDataForm" :rules="addRules" :ref="addDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="addDataForm.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('description')" label-width="70px" prop="description">
                    <el-input v-model="addDataForm.description" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="addData(addDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 修改 -->
        <el-dialog :title="$t('modify')" :visible.sync="modifyDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="modifyDlogClose(modifyDataForm)">
            <el-form :model="modifyDataForm" :rules="modifyRules" :ref="modifyDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="modifyDataForm.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('description')" label-width="70px" prop="description">
                    <el-input v-model="modifyDataForm.description" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="modifyDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="modifyData(modifyDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 删除 -->
        <el-dialog :title="$t('hint')" :visible.sync="delDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>{{$t('confirmdel')}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="delData">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
    getFabricGroupAll,
    addorModifyGroup,
    delGroupItem
} from '@/libs/reqlist.js'
export default {
    data () {
        return {
            total: 1,
            seachForm: {
                pageNo: 1,
                pageSize: 10,
                name: '',
                description: ''
            },
            groupTab: [],
            addDialogVisible: false,
            addDataForm: {
                name: '',
                description: ''
            },
            addRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                description: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ]
            },
            modifyDialogVisible: false,
            modifyDataForm: {
                id: '',
                name: '',
                description: ''
            },
            modifyRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                description: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ]
            },
            delDialogVisible: false,
            delId: 0,
        }
    },
    methods: {
        getGroupList() {
            this.seachForm.pageNo = 1
            this.seachData()
        },
        seachData() {
            getFabricGroupAll(this.seachForm).then(res => {
                if (res.data.code == 0) {
                    this.total = res.data.data.hits
                    this.groupTab = res.data.data.data
                }
            })
        },
        handleSizeChange (newSize) {
            this.seachForm.pageSize = newSize
            this.seachData()
        },
        handleCurrentChange (newNum) {
            this.seachForm.pageNo = newNum
            this.seachData()
        },
        addData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                addorModifyGroup(JSON.stringify(this.addDataForm)).then(res => {
                    if (res.data.code == 0) {
                        this.addDialogVisible = false
                        this.$message({
                            type: 'success',
                            message: this.$t('sucadd')
                        })
                        this.seachData()
                    }
                })
            })
        },
        addDlogClose (form) {
            this.$refs[form].resetFields()
        },
        showModify (item) {
            this.modifyDataForm.id = item.id
            this.modifyDataForm.name = item.name
            this.modifyDataForm.description = item.description
            this.modifyDialogVisible = true
        },
        modifyData (form) {
            this.$refs[form].validate(valid => {
                if (!valid) return
                addorModifyGroup(JSON.stringify(this.modifyDataForm)).then(res => {
                    if (res.data.code == 0) {
                        this.modifyDialogVisible = false
                        this.$message({
                            type: 'success',
                            message: this.$t('modifysuc')
                        })
                        this.seachData()
                    }
                })
            })
        },
        modifyDlogClose (form) {
            this.$refs[form].resetFields()
        },
        showDel (id) {
            this.delId = id
            this.delDialogVisible = true
        },
        delData () {
            delGroupItem(this.delId).then(res => {
                if (res.data.code == 0) {
                    this.delDialogVisible = false
                    this.$message({
                        type: 'success',
                        message: this.$t('delsuc')
                    })
                    this.seachData()
                }
            })
        }
    },
    mounted() {
        this.seachData()
    }
}
</script>

<style lang="less" scoped>
@import './fabgroup.less';
</style>
